/**
 * file
 */
package com.tdkj.action.front.member.friendsManagement;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.friendlylink.BeanFriendlylink;
import com.tdkj.model.user.BeanFriend;
import com.tdkj.model.user.BeanFriendCategory;
import com.tdkj.model.user.BeanMessage;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.user.IBeanMessageService;
import com.tdkj.service.user.IBeanUserService;

/**

 * FileName:     MyMessage.java

 * @Description: TODO我的短消息

 * All rights Reserved, Designed By ZTE-ITS

 * Copyright:    Copyright(C) 2010-2011

 * Company       天成科技有限公司

 * @author:    李兴建

 * @version    V1.0 

 * Createdate:         2012-5-27 下午12:23:01

 *

 * Modification  History:

 * Date         Author        Version        Discription

 * -----------------------------------------------------------------------------------

 * 2012-5-27       李兴建          1.0             1.0

 * Why & What is modified: <修改原因描述>

 */
public class MyMessageAction extends BaseAction {
	private Long id;
	private Long uid;
	private String ctype;//操作类型,0收件箱，1发件箱
	private String messagecontent;
	private List<BeanFriend> friends=new ArrayList<BeanFriend>();
	private String fids;
	private IBeanMessageService mesageService;
	private IBeanUserService userService;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public List<BeanFriend> getFriends() {
		return friends;
	}
	public void setFriends(List<BeanFriend> friends) {
		this.friends = friends;
	}
	public String getFids() {
		return fids;
	}
	public void setFids(String fids) {
		this.fids = fids;
	}
	public Long getUid() {
		return uid;
	}
	public void setUid(Long uid) {
		this.uid = uid;
	}
	public String getCtype() {
		return ctype;
	}
	public void setCtype(String cdtype) {
		this.ctype = cdtype;
	}
	public String getMessagecontent() {
		return messagecontent;
	}
	public void setMessagecontent(String messagecontent) {
		this.messagecontent = messagecontent;
	}
	public IBeanMessageService getMesageService() {
		return mesageService;
	}
	@Resource
	public void setMesageService(IBeanMessageService mesageService) {
		this.mesageService = mesageService;
	}
	public IBeanUserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IBeanUserService userService) {
		this.userService = userService;
	}
	//后去好友列表
	public List<BeanFriend> findFriends(){
		BeanUser u=this.getCurrentUser();
		List<BeanFriendCategory> cl=u.getFriendCategorys();
		for(BeanFriendCategory c:cl){
			List<BeanFriend> fl=c.getFrieneds();
			for(BeanFriend f:fl){
				friends.add(f);
			}
		}
		return friends;
	}
	//获取收件箱
	public String findFromMsg(){
		findFriends();
		BeanUser u=this.getCurrentUser();
		Criteria criteria = this.createCriteria(BeanMessage.class);
		criteria.add(Restrictions.eq("reciver", u));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "myMessageAction!findFromMsg.action", 5);
		return "from";
	}
	//获取发件箱
	public String findToMsg(){
		findFriends();
		BeanUser u=this.getCurrentUser();
		Criteria criteria = this.createCriteria(BeanMessage.class);
		criteria.add(Restrictions.eq("sender", u));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "myMessageAction!findToMsg.action", 5);
		return "to";
	}
	public String deleteMsg(){
		mesageService.delete(this.getId(), BeanMessage.class);
		if(this.getCtype().equals("0")){
			return  findFromMsg();
		}else{
			return  findToMsg();
		}
	}
	public String addMessages(){
		String [] ids=this.getFids().split(",");
		if (null != ids) {
			for (String idtemp : ids) {
				BeanUser ru=this.getBaseService().findById(Long.parseLong(idtemp), BeanUser.class);
				BeanMessage bean=new BeanMessage();
				bean.setContent(this.getMessagecontent());
				bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
				bean.setMsgtype(1);
				bean.setSender(this.getCurrentUser());
				bean.setReciver(ru);
				mesageService.save(bean);
			}
		}
		if(this.getCtype().equals("0")){
			return  findFromMsg();
		}else{
			return  findToMsg();
		}
	}
	
	
	//发送短消息
	public String addMessage(){
		BeanUser ru=userService.findById(this.getUid(),BeanUser.class);
		if(this.getCtype().equals("0")){
			BeanMessage bean=new BeanMessage();
			bean.setContent(this.getMessagecontent());
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			bean.setMsgtype(1);
			bean.setSender(this.getCurrentUser());
			bean.setReciver(ru);
			mesageService.save(bean);
			return  findFromMsg();
		}else{
			return  findToMsg();
		}
	}
}
